package com.neu.admin;

import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.domain.ShoppingAdmin;
import com.neu.service.ShoppingAdminService;
import com.neu.util.bcrypt.BCryptPasswordEncoder;
import com.neu.util.response.BaseResponse;
import com.neu.util.response.ListVoUntil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin/admin")
@Validated
@Api(description = "管理员模块（何凯鑫）")
public class AdminAdminController {
    @Autowired
    private ShoppingAdminService shoppingAdminService;
    private  final Log log = LogFactory.getLog(getClass());

    @ApiOperation(value = "获取管理员信息分页数据",notes = "获取管理员信息分页数据")
    @GetMapping("/list")
    public ResponseEntity<BaseResponse<ListVoUntil<ShoppingAdmin>>> selectByExample(String username,
                                                                                    @RequestParam(defaultValue = "1") Integer page,
                                                                                    @RequestParam(defaultValue = "10") Integer limit,
                                                                                    @Sort @RequestParam(defaultValue = "add_time") String sort,
                                                                                    @Order @RequestParam(defaultValue = "desc") String order)

    {
        try {
            List<ShoppingAdmin> learningList=shoppingAdminService.querySelective(username,page,limit,sort,order);
            return BaseResponse.generateOKListResponseEntity(learningList);
        }catch (Exception e)
        {
            log.debug("LearningFormController-->>getEnumByType",e);
            return BaseResponse.generateBadResponseEntity(500,"服务器内部错误！！","");
        }
    }

    @ApiOperation(value = "增加管理员",notes = "增加管理员")
    @PostMapping("/create")
    public ResponseEntity<BaseResponse<ShoppingAdmin>> create(HttpServletRequest request, @RequestBody ShoppingAdmin admin)
    {
        try {
            ShoppingAdmin adminList = shoppingAdminService.getfindAdmin(admin.getUsername());
            if (adminList!=null) {

                return BaseResponse.generateBadResponseEntity(500,"管理员已经存在！！","");
            }
            String rawPassword = admin.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encodedPassword = encoder.encode(rawPassword);
            admin.setPassword(encodedPassword);
            int flag= shoppingAdminService.add(admin);//调用service层方法
            if (flag==0)
            {
                return BaseResponse.generateBadResponseEntity(500,"学习形式新增失败！！","");

            }
            else {
                return BaseResponse.generateOKResponseEntity("新增成功！");

            }
        }catch (Exception e)
        {
            log.debug("LearningFormController-->>addlearning",e);
            return BaseResponse.generateBadResponseEntity(500,"服务器内部错误！！","");
        }
    }


    @ApiOperation(value = "删除管理员 ",notes = "删除管理员 ")
    @PostMapping("/delete")
    public ResponseEntity<BaseResponse<ShoppingAdmin>> delete(HttpServletRequest request, @RequestBody ShoppingAdmin admin) {
        int resultRow = shoppingAdminService.deleteById(admin.getId());
        if (resultRow == 0) {
            return BaseResponse.generateBadResponseEntity(500, "删除失败！", "");
        }
        return BaseResponse.generateOKResponseEntity("删除成功！");
    }
    @ApiOperation(value = "修改管理员信息",notes = "修改管理员信息")
    @PostMapping("/update")
    public ResponseEntity<BaseResponse<ShoppingAdmin>> update(@RequestBody ShoppingAdmin shoppingAdmin)throws Exception{
        ShoppingAdmin shoppingAdminDB=shoppingAdminService.getSchoolSystemByName(shoppingAdmin);
        if (shoppingAdminDB!=null){
            return BaseResponse.generateBadResponseEntity(500,"管理员已经存在！","");
        }
        String rawPassword = shoppingAdmin.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(rawPassword);
        shoppingAdmin.setPassword(encodedPassword);
        int resultRow=shoppingAdminService.updateById(shoppingAdmin);
        if (resultRow==0){
            return BaseResponse.generateBadResponseEntity(500,"更新失败！","");
        }
        return BaseResponse.generateOKResponseEntity("更新成功！");
    }
}
